﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace AvvaMobileNHibernateHelperKit.Controls
{
    public partial class Connection : UserControl
    {
        public Connection()
        {
            InitializeComponent();
        }


        private void SetObjectToPropertiesWindow()
        {
            MainForm mf = (MainForm)this.FindForm();
            AvvaMobileNHibernateHelperKit.Objects.PropertiesWindow pw = (AvvaMobileNHibernateHelperKit.Objects.PropertiesWindow)mf.Controls["pnlMain"].Controls["propertiesWindow1"];
            pw.SelectedObject = new ConnectionPropertyManager();
            pw.Refresh();
        }
        
        
        


        private void Connection_Load(object sender, EventArgs e)
        {
        }

        private void ShowSmartTask()
        {
            pnlSmartTask.Visible = true;
        }

        private void HideSmartTask()
        {
            pnlSmartTask.Visible = false;
        }

        private void imgSmartTasks_Click(object sender, EventArgs e)
        {
            SetObjectToPropertiesWindow();
            if (pnlSmartTask.Visible)
            {
                HideSmartTask();
            }
            else 
            {
                ShowSmartTask();
            }            
        }

        private void pnlConnection_Click(object sender, EventArgs e)
        {
            HideSmartTask();
            SetObjectToPropertiesWindow();
        }

        private void Connection_Click(object sender, EventArgs e)
        {
            HideSmartTask();
            SetObjectToPropertiesWindow();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            HideSmartTask();
            SetObjectToPropertiesWindow();
        }

        private void lnkSetConnection_Click(object sender, EventArgs e)
        {
            SetObjectToPropertiesWindow();
            System.Diagnostics.Process.Start(Application.StartupPath + @"\settings\connection.udl");
        }

        private void lnkLoadConnection_Click(object sender, EventArgs e)
        {
            try
            {
                SetObjectToPropertiesWindow();
                this.Cursor = Cursors.WaitCursor;
                
                System.IO.StreamReader sr = new System.IO.StreamReader(Application.StartupPath + @"\settings\connection.udl");
                sr.ReadLine();
                sr.ReadLine();

                DbFactory dbFactory = new DbFactory();
                ProjectSettings.ConnectionString = sr.ReadLine();
                dbFactory.TestConnection();

                MainForm mf = (MainForm)this.FindForm();
                AvvaMobileNHibernateHelperKit.Objects.PropertiesWindow pw = (AvvaMobileNHibernateHelperKit.Objects.PropertiesWindow)mf.Controls["pnlMain"].Controls["propertiesWindow1"];
                pw.Refresh();

                Dialogs.ShowInfoBox("Load connection succeeded.", "Connection");
            }
            catch (Exception ex)
            {
                Dialogs.ShowErrorBox(ex.Message, "Connection");
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

       




    }
}
